package cc.bukkitPlugin.banitem.command;

import java.util.ArrayList;
import java.util.Arrays;

import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import cc.bukkitPlugin.banitem.BanItem;
import cc.bukkitPlugin.banitem.CFG;
import cc.bukkitPlugin.banitem.check.ItemCheck;
import cc.bukkitPlugin.util.CCBukkit;
import cc.bukkitPlugin.util.plugin.command.TACommandBase;

public class CommandScan extends TACommandBase<BanItem>{

    public CommandScan(CommandExc pExector){
        super(pExector,"scan");
    }

    @Override
    public boolean execute(CommandSender pSender,String pLabel,String[] pArgs){
        if(!pSender.hasPermission(this.mPluginName+"."+this.getCommandLabel()))
            return noPermission(pSender);
        ArrayList<Player> scanPlayers=new ArrayList<>();
        if(pArgs.length==0){
            scanPlayers.addAll(Arrays.asList(CCBukkit.getOnlinePlayers()));
        }else if(pArgs.length==1){
            Player tPlayer=Bukkit.getPlayer(pArgs[0]);
            if(tPlayer==null)
                return send(pSender,"&c玩家["+pArgs[0]+"]未在线或不存在");
            scanPlayers.add(tPlayer);
        }else return errorArgsNumber(pSender,pArgs.length);
        if(scanPlayers.size()==0)
            return send(pSender,"&c当前没有任何玩家在线");
        ItemCheck tcheckor=this.mPlugin.getManager(ItemCheck.class);
        for(Player sPlayer : scanPlayers){
            if(sPlayer==null)
                continue;
            tcheckor.checkPlayerInv(sPlayer,pSender);
        }
        return true;
    }

    @Override
    public ArrayList<String> getHelp(CommandSender pSender,String pLabel){
        ArrayList<String> list=new ArrayList<>();
        if(pSender.hasPermission(this.mPluginName+".scan [玩家名]")){
            list.add(CFG.cmd_color+this.mMainCmdLabel+" scan");
            list.add(CFG.help_color+"扫描玩家背包,检查是否有黑名单物品");
            list.add(CFG.help_color+"如果未指定玩家,将扫描全部在线玩家");
            list.add(CFG.help_color+"此时可能会消耗较长时间,导致服务器大面积掉线");
        }
        return list;
    }

    @Override
    public ArrayList<String> getSubCommand(CommandSender pSender,String pLabel,String[] pArgs){
        ArrayList<String> players=new ArrayList<>();
        for(Player sPlayer : CCBukkit.getOnlinePlayers()){
            if(sPlayer==null)
                continue;
            players.add(sPlayer.getName());
        }
        return players;
    }

    @Override
    public int getMaxArgumentLength(String pLabel){
        return 1;
    }

}
